const moment = require('moment')
    // 获取两个时间段的所有时间
const getTwoMinList = (beginTime, endTime) => {
    let intervaltime = (new Date(endTime).getTime() - new Date(beginTime).getTime()) / 60000
    let timeList = []
    for (let i = 0; i < intervaltime + 1; i++) {
        let time = new Date(beginTime).getTime() + i * 60000
        timeList.push(time)
    }
    return timeList.map(function(item, index, input) {
        let date = new Date(item + 8 * 3600 * 1000)
            // 这里的 - 可以按照需要改为 /. 等等其他符号
        return date.toJSON().substr(0, 19).replace('T', ' ').replace(/-/g, '-')
    })
}

// 获取两个时间段的所有时间
const getTwoMinListByCollectionfrequency = (beginTime, endTime,collectionfrequency) => {
    if("" == collectionfrequency || null == collectionfrequency){
        collectionfrequency = 15

    }
    var density = parseInt(collectionfrequency)
    let intervaltime = (new Date(endTime).getTime() - new Date(beginTime).getTime()) / 60000 / density
    let timeList = []
    for (let i = 0; i < intervaltime + 1; i++) {
        let time = new Date(beginTime).getTime() + i * 60000 * density
        timeList.push(time)
    }
    return timeList.map(function(item, index, input) {
        let date = new Date(item + 8 * 3600 * 1000)
        // 这里的 - 可以按照需要改为 /. 等等其他符号
        return date.toJSON().substr(0, 19).replace('T', ' ').replace(/-/g, '-')
    })
}
/**
 * [dateAddDays 从某两个日期之间的所有时间]
 * @param  {[string]} dateStr  [日期字符串]
 * @param  {[int]} dayCount [增加的天数]
 * @return {[string]}[增加n天后的日期字符串]
 */
const dateAddDays = (dateStr, dayCount) => {
    let tempDate = new Date(dateStr.replace(/-/g, "/")) //把日期字符串转换成日期格式
    let resultDate = new Date((tempDate / 1000 + (86400 * dayCount)) * 1000) //增加n天后的日期
    let resultDateStr = resultDate.getFullYear() + "-" + (resultDate.getMonth() + 1) + "-" + (resultDate
            .getDate()) //将日期转化为字符串格式
    return resultDateStr
}

/**
 * [getTwoDaysNumber 从某个日期增加n天后的日期]
 * @param  {[string]} a  [日期字符串]
 * @param  {[string]} b [日期字符串]
 * @return {[string]}[相差天数字符串]
 */
const getTwoDaysNumber = (a, b) => {
    let startTime = new Date(a)
    let endTime = new Date(b)
    let usedTime = endTime - startTime
    return Math.floor(usedTime / (24 * 3600 * 1000)) // 计算出天数
}

// 计算相隔小时数
const getTwoTimeList = (beginTime, endTime) => {
    let intervaltime = (new Date(endTime).getTime() - new Date(beginTime).getTime()) / 3600000
    let timeList = []
    for (let i = 0; i < intervaltime + 1; i++) {
        let time = new Date(beginTime).getTime() + i * 1000 * 60 * 60
        timeList.push(time)
    }
    return timeList.map((item, index, input) => {
        let date = new Date(item + 8 * 3600 * 1000)
            // 这里的 - 可以按照需要改为 /. 等等其他符号
        return date.toJSON().substr(0, 19).replace('T', ' ').replace(/-/g, '-')
    })
}

// 计算相隔天数
const getTwoDayList = (beginTime, endTime) => {
    let result = []
    let beginDay = beginTime.split("-")
    let endDay = endTime.split("-")
    let diffDay = new Date()
    let dateList = new Array
    let i = 0
    diffDay.setDate(beginDay[2])
    diffDay.setMonth(beginDay[1] - 1)
    diffDay.setFullYear(beginDay[0])
    result.push(beginTime)
    while (i == 0) {
        let countDay = diffDay.getTime() + 24 * 60 * 60 * 1000
        diffDay.setTime(countDay)
        dateList[2] = diffDay.getDate()
        dateList[1] = diffDay.getMonth() + 1
        dateList[0] = diffDay.getFullYear()
        if (String(dateList[1]).length == 1) {
            dateList[1] = "0" + dateList[1]
        }
        if (String(dateList[2]).length == 1) {
            dateList[2] = "0" + dateList[2]
        }
        result.push(dateList[0] + "-" + dateList[1] + "-" + dateList[2])
        if (dateList[0] == endDay[0] && dateList[1] == endDay[1] && dateList[2] == endDay[2]) {
            i = 1
        }
    }
    return result
}

// 计算相隔月数
const getTwoMonth = (beginTime, endTime) => {
    let result = []
    let s = beginTime.split("-")
    let e = endTime.split("-")
    let min = new Date()
    let max = new Date()
    let yearMonthCode
    min.setFullYear(s[0], s[1] * 1 - 1, 1); //开始日期
    max.setFullYear(e[0], e[1] * 1 - 1, 1); //结束日期
    var curr = min
    while (curr <= max) {
        yearMonthCode = moment(curr).format('YYYY-MM')
        let month = curr.getMonth()
        let year = curr.getFullYear()

        let str = curr.getFullYear() + "-" + (month)
        let sa = curr.getFullYear() + "-0"
        if (str == sa) {
            str = curr.getFullYear() + "-1"
        }
        let m = month + 1
        result.push(yearMonthCode)
        curr.setMonth(month + 1)
    }
    return result
}

const getTwoYear = (startYear, endYear) => {
    let yearArr = [],
        prDate = new Date(),
        presentYear = prDate.getFullYear() //当前年份
    if (!endYear) { //为空为当前年份
        endYear = presentYear
    }
    if (!startYear) { //为空为当前年前5年
        startYear = presentYear - 5
    }
    for (let i = startYear; i <= endYear; i++) {
        yearArr.push(`${i}`)
    }
    return yearArr
}

// 计算时间间隔
/* eslint-disable */
const countTimeInterval = (startTime, endTime) => {
    if (startTime == undefined || endTime == undefined) {
        return 99999999
    }
    // 开始时间
    let d1 = startTime.replace(/\-/g, "/")
    let date1 = new Date(d1)
        // 结束时间
    let d2 = endTime.replace(/\-/g, "/")
    let date2 = new Date(d2)
        // 时间相差秒数
    let dateDiff = Math.abs(date2.getTime() - date1.getTime())
        // 计算出相差天数
    let days = Math.abs(Math.floor(dateDiff / (24 * 3600 * 1000)))
        // 计算出小时数
    let residue1 = Math.abs(dateDiff % (24 * 3600 * 1000)) // 计算天数后剩余的毫秒数
    let hours = Math.abs(Math.floor(residue1 / (3600 * 1000)))
        // 计算相差分钟数
    let residue2 = Math.abs(residue1 % (3600 * 1000)) // 计算小时数后剩余的毫秒数
    let minutes = Math.abs(Math.floor(residue2 / (60 * 1000)))
        // 计算相差秒数
    let residue3 = Math.abs(residue2 % (60 * 1000)) // 计算分钟数后剩余的毫秒数
    let seconds = Math.abs(Math.round(residue3 / 1000))

    let daysMin = days * 1440
    let hourMin = hours * 60
    let Min = minutes
    let minutesMin = seconds / 60
    let returnVal = daysMin + hourMin + Min + minutesMin
    return returnVal
}

// 计算间隔时间倍数
const countTimeMultiple = (beginTime, endTime, n) => {
    let intervaltime = (new Date(endTime).getTime() - new Date(beginTime).getTime()) / (60000 * n)
    let timeList = []
    for (let i = 0; i < intervaltime + 1; i++) {
        let time = new Date(beginTime).getTime() + i * (60000 * n)
        timeList.push(time)
    }
    return timeList.map(function(item, index, input) {
        let date = new Date(item + 8 * 3600 * 1000)
            // 这里的 - 可以按照需要改为 /. 等等其他符号
        return date.toJSON().substr(0, 19).replace('T', ' ').replace(/-/g, '-')
    })
}

// 计算间隔时间,一天,频率分钟
const countTimeMultipleForDay = (n) => {
    let intervaltime = 24 * 60  / n
    let timeList = []
    let beginTime = '2022-07-21 00:00:00'
    for (let i = 0; i < intervaltime + 1; i++) {
        let time = new Date(beginTime).getTime() + i * (60000 * n);
        timeList.push(time);
    }
    let timeArray= timeList.map(function(item, index, input) {
        let date = new Date(item + 8 * 3600 * 1000);
        // 这里的 - 可以按照需要改为 /. 等等其他符号
        return date.toJSON().substr(11, 5).replace('T', ' ').replace(/-/g, '-');

    })

    timeArray.pop();
    // console.log("数组得到数据===》",timeArray)
    return timeArray;
}

// 计算间隔时间倍数
const getIndexInTimeMultipleForDay = (endTime, n) => {
    let beginTime = endTime.substr(0,10)+" 00:00:00"
    let intervaltime = Math.floor((new Date(endTime).getTime() - new Date(beginTime).getTime()) / (60000 * n))
    return intervaltime
}

// export function GetPreMonthDay(date, monthNum) {
//   var dateArr = date.split('-');
//   var year = dateArr[0]; //获取当前日期的年份
//   var month = dateArr[1]; //获取当前日期的月份
//   var day = dateArr[2]; //获取当前日期的日
//   var days = new Date(year, month, 0);
//   days = days.getDate(); //获取当前日期中月的天数
//   var year2 = year;
//   var month2 = parseInt(month) - monthNum;
//   if (month2 <= 0) {
//     var absM = Math.abs(month2);
//     year2 = parseInt(year2) - Math.ceil(absM / 12 == 0 ? 1 : parseInt(absM) / 12);
//     month2 = 12 - (absM % 12);
//   }
//   var day2 = day;
//   var days2 = new Date(year2, month2, 0);
//   days2 = days2.getDate();
//   if (day2 > days2) {
//     day2 = days2;
//   }
//   if (month2 < 10) {
//     month2 = '0' + month2;
//   }
//   var t2 = year2 + '-' + month2 + '-' + day2;
//   return t2;
// }
// 获取时间段内每天的时间并放入新数组
const generdateDatesgenerateDates = (startDate, endDate) => {
  const dates = [];
  let currentDate = new Date(startDate);
  while (currentDate <= new Date(endDate)) {
    const formattedDate = currentDate.toISOString().split('T')[0];
    dates.push(formattedDate);
    currentDate.setDate(currentDate.getDate() + 1);
  }
  return dates;
}

// 获取一个时间段内的所有月份
const getMonths = (startDate, endDate) => {
  const start = moment(startDate);
  const end = moment(endDate);
  const arr = [start.format('YYYY-MM')];
  const diff = moment(end.format('YYYY-MM')).diff(moment(start).format('YYYY-MM'), 'month');//需要转为年月格式再进行diff
  for (let i = 1; i <= diff; i++) {
    arr.push(start.add(1, 'month').format('YYYY-MM'));
  }
  return arr;
}
module.exports = {
  getTwoMinList,
  getTwoMinListByCollectionfrequency,
  dateAddDays,
  getTwoDaysNumber,
  getTwoTimeList,
  getTwoDayList,
  getTwoMonth,
  getTwoYear,
  countTimeInterval,
  countTimeMultipleForDay,
  getIndexInTimeMultipleForDay,
  countTimeMultiple,
  generdateDatesgenerateDates,
  getMonths
}
